package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.TradeAreatInfoMapper;
import cc.rengu.igas.bomp.common.entity.TradeAreatInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商圈信息表操作接口实现类
 * Created by 赵奎伍
 */
public class TradeAreatInfoMapperImpl implements TradeAreatInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);


    @Override
    public List<TradeAreatInfo> selectTradeAreatInfoByInstId(String instId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectTradeAreatInfoByInstId");
        TradeAreatInfo tradeAreatInfo = new TradeAreatInfo();
        tradeAreatInfo.setInstId(instId);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, tradeAreatInfo, TradeAreatInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取商圈信息失败,instId:<{}>", instId);
            return null;
        }
        return objectList.stream().map(item -> (TradeAreatInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<TradeAreatInfo> selectTradeAreatInfoByKeys(String instId, String mchntArea, String mchntAreaName, String areaCode, String signInstId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectTradeAreatInfoByKeys");
        TradeAreatInfo tradeAreatInfo = new TradeAreatInfo();
        tradeAreatInfo.setInstId(instId);
        if (null != mchntArea && !mchntArea.isEmpty()) {
            tradeAreatInfo.setTradeAreaCode(mchntArea);
        }
        if (null != mchntAreaName && !mchntAreaName.isEmpty()) {
            tradeAreatInfo.setTradeAreaName(mchntAreaName);
        }
        if (null != areaCode && !areaCode.isEmpty()) {
            tradeAreatInfo.setBelongAreaCode(areaCode);
        }
        if (null != signInstId && !signInstId.isEmpty()) {
            tradeAreatInfo.setBelongInstId(signInstId);//todo 待确认是否是此字段
        }
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, tradeAreatInfo, TradeAreatInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据商户标识、商圈名称、所属地区代码查询失败,instId:<{}>,mchntArea:<{}>,mchntAreaName:<{}>,areaCode:<{}>,signInstId:<{}>", instId, mchntArea, mchntAreaName, areaCode, signInstId);
            return null;
        }
        return objectList.stream().map(item -> (TradeAreatInfo) item).collect(Collectors.toList());
    }

    @Override
    public PageResult selectTradeAreatInfoByKeysByPage(String instId, String mchntArea, String mchntAreaName, String areaCode, String signInstId, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectTradeAreatInfoByKeys");
        TradeAreatInfo tradeAreatInfo = new TradeAreatInfo();
        tradeAreatInfo.setInstId(instId);
        if (null != mchntArea && !mchntArea.isEmpty()) {
            tradeAreatInfo.setTradeAreaCode(mchntArea);
        }
        if (null != mchntAreaName && !mchntAreaName.isEmpty()) {
            tradeAreatInfo.setTradeAreaName(mchntAreaName);
        }
        if (null != areaCode && !areaCode.isEmpty()) {
            tradeAreatInfo.setBelongAreaCode(areaCode);
        }
        if (null != signInstId && !signInstId.isEmpty()) {
            tradeAreatInfo.setBelongInstId(signInstId);//todo 待确认是否是此字段
        }
        PageResult pageResult = dbsUtil.dbsFetchPageListData(sqlStr, tradeAreatInfo, TradeAreatInfo.class, pageInfo);
        if (null == pageResult) {
            rglog.error("根据商户标识、商圈名称、所属地区代码查询失败,instId:<{}>,mchntArea:<{}>,mchntAreaName:<{}>,areaCode:<{}>,signInstId:<{}>", instId, mchntArea, mchntAreaName, areaCode, signInstId);
            return null;
        }
        return pageResult;
    }

    @Override
    public PageResult selectTradeAreatInfoByInstIdByPage(String instId, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectTradeAreatInfoByInstId");
        TradeAreatInfo tradeAreatInfo = new TradeAreatInfo();
        tradeAreatInfo.setInstId(instId);
        PageResult pageResult = dbsUtil.dbsFetchPageListData(sqlStr, tradeAreatInfo, TradeAreatInfo.class, pageInfo);
        if (null == pageResult) {
            rglog.error("获取商圈信息失败,instId:<{}>", instId);
            return null;
        }
        return pageResult;
    }
}